<?php

if (!defined('CONFIGURATION_FILE_LOADED'))
    include( 'includes/configuration_include.php');


include( PATH_VIEWS . 'control_slide_view.php');
include( PATH_MODELS . 'control_slide_model.php');

$bdd = model_db_connexion();
// Si l'utilisateur est deja logue
if (!empty($_SESSION['id_user'])) {

    echo'<span class="template_title">Gestion du slide</span>';

    if (isset($_POST['form']) == 1) {
        echo '<span class="template_span">Le slide est a present actualisé</span>';
        $i = 1;

        model_delete_slide($bdd);

        while ($i < 16) {
            $choice_slide = $_POST['slide' . $i . ''];
            $testtype = model_search_optgroup_works($choice_slide, $bdd);
        if($choice_slide != 'Vide'){  if ( $testtype == 0){
                            $type = 'news';
                           

            }
            else {$type = 'works';
                        

            }
            model_insert_slide($bdd, $i, $choice_slide, $type);

            $i++;
        }else{$i++;
       
        }}
    } elseif (isset($_POST['form']) == 2) {
        $name = $_POST["name"];
        $count_extra = count_extra($bdd);
        if (upload_photo($count_extra + 1, $type_img) != false) {
            $type_img = upload_photo($count_extra + 1, $type_img);
            model_add_extra($bdd, $name, $type_img);
        } else {
            echo 'ce n est pas le bon format ';
        }
    } else {
        $i = 1;
        start_form();

        while ($i < 16) {
           $req = select_print_works($bdd);
            $req1 = select_print_news($bdd);
            $req2 = select_slide($bdd, $i);

            start_slide($i);
            void_option();
            
            echo ' <optgroup id = "2" label = "Oeuvre">';

$num_slide = 0;
            while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
                $name = $donnees['work_name'];

                while ($donnees = $req2->fetch(PDO::FETCH_ASSOC)) {
                    $name_slide = $donnees['slide_name'];
                  $num_slide = $donnees['num_slide'];

                }
           
 if($i == $num_slide){               
                    option_work($name, $name_slide);
                }
                else {
                    option_work($name, 'Vide');
                }
            }
            echo '</optgroup>';



            echo ' <optgroup label = "Actualité">';
           

            while ($donnees = $req1->fetch(PDO::FETCH_ASSOC)) {
                $new = $donnees['new_name'];

     while ($donnees = $req2->fetch(PDO::FETCH_ASSOC)) {
                    $name_slide = $donnees['slide_name'];
                  $num_slide = $donnees['num_slide'];

                }
           
 if($i == $num_slide){
                
                    option_new($new, $name_slide);
                }
                else {
                    option_new($new, 'Vide');
                }
               
            }
            echo '</optgroup>';

            echo ' </select>';

            $i++;
        }
        echo' </br></br><input class="button" type="submit" value="Valider"> 

</FORM>';
    }

    function model_search_optgroup($name, $bdd) {
        $req = select_print_works($bdd);

        while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
            if ($name == $donnees['work_name']) {
                return 'works';
            }
        }
        $req1 = select_print_news($bdd);

        while ($donnees = $req1->fetch(PDO::FETCH_ASSOC)) {
            if ($name == $donnees['new_name']) {
                return 'news';
            }
        }
    }

    function upload_photo($count_extra) {

        if (isset($_FILES['photo']) AND $_FILES['photo']['error'] == 0) {

            if ($_FILES['photo']['size'] <= 7000000) {

                $infosfichier = pathinfo($_FILES['photo']['name']);

                $extension_upload = $infosfichier['extension'];

                $extensions_autorisees = array('jpeg', 'png');

                if (in_array($extension_upload, $extensions_autorisees)) {




                    //$name= $id_work .".". $extension_upload;

                    move_uploaded_file($_FILES['photo']['tmp_name'], "design/img/extra/" . $count_extra . "." . $extension_upload . "");
                    if ($extension_upload == 'png') {
                        return 1;
                    } else {
                        return 0;
                    }
                } else {
                    return false;
                }
            }
        }
    }

} else {
    echo '<span class="template_span">' . LANG_NO_CONNECT . '</span>';
}
?>
